<template>
  <div>
    <ay-aside :page-info="pageInfo" />
    <div class="app-container">
      <div class="filter-container">
        <el-collapse v-model="filterNameKey" @change="handleChange">
          <el-collapse-item title="筛选条件" name="1">
            <el-row>
              <el-input v-model="search.shopName" placeholder="店铺名称" style="width: 200px;" class="filter-item" />
              <el-input v-model="search.memberId" placeholder="会员ID" style="width: 200px;" class="filter-item" />
              <el-select v-model="search.auditStatus" placeholder="签约状态" clearable style="width: 200px" class="filter-item">
                <el-option :key="0" label="审核成功" :value="3" />
                <el-option :key="1" label="待绑卡" :value="50" />
                <el-option :key="2" label="待签约" :value="60" />
                <el-option :key="3" label="待开通支付" :value="70" />
              </el-select>
              <el-date-picker
                v-model="submitTimeTemp"
                class="filter-item"
                type="datetimerange"
                range-separator="至"
                start-placeholder="提交开始日期"
                end-placeholder="提交结束日期"
              />
            </el-row>
          </el-collapse-item>
        </el-collapse>
        <el-row>
          <el-button v-waves class="filter-item refresh-btn" icon="el-icon-refresh" @click="handleRefresh" />
          <transition name="el-zoom-in-center">
            <el-button v-show="isShowFilterBtn" v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleSearch">
              搜索
            </el-button>
          </transition>
          <transition name="el-zoom-in-center">
            <el-button v-show="isShowFilterBtn" v-waves class="filter-item" type="danger" icon="el-icon-close" @click="handleReset">
              重置
            </el-button>
          </transition>
        </el-row>
      </div>
      <el-table v-loading="listLoading" :data="tableData" border highlight-current-row style="width: 100%;">
        <el-table-column label="#" align="center" width="60">
          <template slot-scope="{$index}">
            <span>{{ $index + 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="会员ID" align="center" min-width="100px">
          <template slot-scope="{row}">
            <span>{{ row.memberId }}</span>
          </template>
        </el-table-column>
        <el-table-column label="店铺名称" align="center" min-width="100px">
          <template slot-scope="{row}">
            <span>{{ row.shopName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="客户类型" align="center" width="100px">
          <template slot-scope="{row}">
            <span>{{ row.cMS013Data.customertype|filterShopCate }}</span>
          </template>
        </el-table-column>
        <el-table-column label="店铺地址" align="center" min-width="120px">
          <template slot-scope="{row}">
            <span>{{ row.cMS013Data.address }}</span>
          </template>
        </el-table-column>
        <el-table-column label="商户号" align="center" min-width="110px">
          <template slot-scope="{row}">
            <span>{{ row.ua_mapp_no }}</span>
          </template>
        </el-table-column>
        <el-table-column label="企业号" align="center" min-width="110px">
          <template slot-scope="{row}">
            <span>{{ row.ua_company_no }}</span>
          </template>
        </el-table-column>
        <el-table-column label="提交时间" align="center" width="160px">
          <template slot-scope="{row}">
            <span>{{ row.createTime|filterTime }}</span>
          </template>
        </el-table-column>
        <el-table-column label="审核备注" align="center" width="120px">
          <template slot-scope="{row}">
            <span>{{ row.auditDescription }}</span>
          </template>
        </el-table-column>
        <el-table-column label="签约状态" align="center" width="120px">
          <template slot-scope="{row}">
            <span>{{ row.auditStatus|filterAuditStatus }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="140" class-name="small-padding fixed-width">
          <template slot-scope="{row}">
            <span v-show="row.auditStatus === 3">无</span>
            <el-button v-show="row.auditStatus === 60" style="margin-left:0;" type="primary" size="mini" plain @click="handleUpdate(row.ua_mapp_no)">尝试更新</el-button>
            <el-button v-show="row.auditStatus !== 3" style="margin-left:0;" type="danger" size="mini" plain @click="handleDel(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="pageTotal > 0" :total="pageTotal" :page.sync="pageNum" :limit.sync="pageSize" @pagination="getList" />
      <el-dialog
        title="提示"
        :visible.sync="dialogDelVisible"
        width="30%"
      >
        <span>确定删除数据？</span>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogDelVisible = false">取 消</el-button>
          <el-button type="primary" @click="deleteData">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { ztMerchantNoList, ztBusinessUpdate, ztMerchantNoDel } from '@/api/merchant'
import AyAside from '@/components/Aside'
import Pagination from '@/components/Pagination'
import waves from '@/directive/waves' // waves directive
export default {
  name: 'MerchantCIC',
  components: { AyAside, Pagination },
  directives: { waves },
  filters: {
    filterShopCate(val) {
      let cate = '未知类型'
      switch (val) {
        case '1':
          cate = '个体工商户'
          break
        case '2':
          cate = '企业商户'
          break
        case '3':
          cate = '小微商户'
          break
      }
      return cate
    },
    filterTime(date) {
      if (date) {
        const dateV = new Date(date * 1000)
        var year = dateV.getFullYear()
        var month = dateV.getMonth() + 1
        var day = dateV.getDate()
        var hour = dateV.getHours()
        var minute = dateV.getMinutes()
        var second = dateV.getSeconds()
        if (String(month).length <= 1) {
          month = '0' + month
        }
        if (String(day).length <= 1) {
          day = '0' + day
        }
        if (String(hour).length <= 1) {
          hour = '0' + hour
        }
        if (String(minute).length <= 1) {
          minute = '0' + minute
        }
        if (String(second).length <= 1) {
          second = '0' + second
        }
        return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second
      }
    },
    filterAuditStatus(val) {
      let resName = '未知审核状态'
      switch (val) {
        case 3:
          resName = '审核成功'
          break
        case 50:
          resName = '待绑卡'
          break
        case 60:
          resName = '待签约'
          break
        case 70:
          resName = '待开通支付'
          break
      }
      return resName
    }
  },
  data() {
    return {
      pageInfo: {},
      isShowFilterBtn: true,
      filterNameKey: '1',
      submitTimeTemp: '', // 提交时间中间量
      search: {
        shopName: '', // 店铺名称
        memberId: '', // 会员id
        auditStatus: '', // 签约状态
        submitTime: '' // 提交时间
      },
      listLoading: false,
      tableData: [],
      pageNum: 1,
      pageSize: 10,
      pageTotal: 0,
      dialogDelVisible: false,
      delId: ''
    }
  },
  watch: {
    submitTimeTemp(newval, oldval) {
      if (!newval) {
        this.search.submitTime = ''
      } else {
        const start = new Date(this.submitTimeTemp[0])
        const resStartDate = start.getFullYear() + '-' + this.doTwoDate((start.getMonth() + 1)) + '-' + this.doTwoDate(start.getDate())
        const resStartTime = this.doTwoDate(start.getHours()) + ':' + this.doTwoDate(start.getMinutes()) + ':' + this.doTwoDate(start.getSeconds())
        const end = new Date(this.submitTimeTemp[1])
        const resEndDate = end.getFullYear() + '-' + this.doTwoDate((end.getMonth() + 1)) + '-' + this.doTwoDate(end.getDate())
        const resEndTime = this.doTwoDate(end.getHours()) + ':' + this.doTwoDate(end.getMinutes()) + ':' + this.doTwoDate(end.getSeconds())
        this.search.submitTime = `${resStartDate} ${resStartTime}-${resEndDate} ${resEndTime}`
      }
    }
  },
  created() {
    this.pageInfo = this.$route.meta
    this.getList()
  },
  methods: {
    // 搜索折叠面板
    handleChange(val) {
      if (val.length === 0) {
        this.isShowFilterBtn = false
      } else {
        this.isShowFilterBtn = true
      }
    },
    // 刷新
    async handleRefresh() {
      if (await this.getList()) {
        this.$message({
          message: '刷新成功',
          type: 'success'
        })
      }
    },
    // 筛选查询操作
    handleSearch() {
      this.pageNum = 1
      this.getList()
    },
    // 重置操作
    handleReset() {
      this.search = {
        shopName: '', // 店铺名称
        memberId: '', // 会员id
        auditStatus: '', // 签约状态
        submitTime: '' // 提交时间
      }
      this.submitTimeTemp = ''
      this.pageNum = 1
      this.getList()
    },
    // 处理日期
    doTwoDate(s) {
      return s < 10 ? '0' + s : s
    },
    // 获取表格数据接口
    getList() {
      this.listLoading = true
      return new Promise((resolve, reject) => {
        const param = {}
        Object.assign(param, {
          page: this.pageNum,
          limit: this.pageSize
        }, this.search)
        ztMerchantNoList(param).then(response => {
          this.tableData = response.data.list
          this.pageTotal = response.data.total
          this.listLoading = false
          resolve(true)
        }).catch(error => {
          this.$message({
            message: error,
            type: 'error'
          })
          reject(false)
        })
      })
    },
    // 删除操作
    handleDel(id) {
      this.dialogDelVisible = true
      this.delId = id
    },
    // 删除接口
    deleteData() {
      ztMerchantNoDel({ id: this.delId }).then(() => {
        this.dialogDelVisible = false
        this.$message({
          message: '删除成功！',
          type: 'success'
        })
        this.getList()
      })
    },
    // 尝试更新操作
    handleUpdate(nums) {
      ztBusinessUpdate({ merId: nums }).then(() => {
        this.$message({
          message: '更新成功！',
          type: 'success'
        })
        this.getList()
      })
    }
  }
}
</script>

<style lang="scss">
.app-container {
  .el-table {
    .gray-row {
      color: #c2d2d2;
    }
  }
  .el-button--mini{
    padding: 7px;
  }
  .filter-container{
    .el-collapse {
      margin-bottom: 10px;
      .el-collapse-item {
        .el-collapse-item__header {
          font-weight: 600;
        }
      }
    }
    .filter-item {
      margin-left: 0;
      &.refresh-btn{
        background-color: #030303;
        color: azure;
      }
    }
  }
}
</style>

